﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace YJKExcelTableUtil
{
	internal class TextCalculation
	{
		public static bool LoadXml(string strXmlPath, ref XmlNode xnRoot)
		{
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				xmlDocument.Load(strXmlPath);
			}
			catch
			{
				return false;
			}
			xnRoot = xmlDocument.SelectSingleNode("root");
			return xnRoot != null;
		}

		public static void ReadXmlData(XmlNode xnRoot, string str, ref TextData textData)
		{
			XmlNode xmlNode = TextCalculation.FindNode(xnRoot, "FontName", textData.FontName);
			if (xmlNode != null)
			{
				string str2 = textData.FontSize.ToString();
				XmlNode xmlNode2 = TextCalculation.FindNode(xmlNode, "size", str2);
				if (xmlNode2 != null)
				{
					XmlNode xmlNode3 = TextCalculation.FindNode(xmlNode2, "type", str);
					if (xmlNode3 != null)
					{
						TextCalculation.GetTextData(xmlNode3, ref textData);
					}
				}
			}
		}

		public static XmlNode FindNode(XmlNode node, string strAttributes, string str)
		{
			foreach (object obj in node.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode.Attributes[strAttributes].Value == str)
				{
					return xmlNode;
				}
			}
			return null;
		}

		public static void GetTextData(XmlNode node, ref TextData textData)
		{
			try
			{
				XmlAttributeCollection attributes = node.Attributes;
				if (attributes["Equidistant"].Value == "是")
				{
					string value = attributes["value"].Value;
					textData.Equidistant = value.ToDouble(0.0);
				}
				XmlNode xmlNode = node.SelectSingleNode("FirstTextSize");
				textData.FirstTextSize = xmlNode.InnerText.ToDouble(0.0);
				xmlNode = node.SelectSingleNode("AddSize");
				textData.AddSize = xmlNode.InnerText.ToDouble(0.0);
				xmlNode = node.SelectSingleNode("Index");
				textData.Index = xmlNode.InnerText.ToInt(0);
				xmlNode = node.SelectSingleNode("CellWitdh");
				textData.CellWitdh = xmlNode.InnerText.ToDouble(0.0);
			}
			catch (Exception)
			{
			}
		}

		public static double TextWidthCalculation(List<int> textType, TextData textDataEOrN, TextData textDataC)
		{
			double num = 0.0;
			int num2 = 0;
			int num3 = 0;
			using (List<int>.Enumerator enumerator = textType.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current == 63)
					{
						num3++;
					}
					else
					{
						num2++;
					}
				}
			}
			if (num3 != 0)
			{
				TextCalculation.CalculationC(ref num, num3, textDataC);
			}
			if (num2 != 0)
			{
				TextCalculation.CalculationEOrN(ref num, num2, textDataEOrN);
			}
			return num + 1.0;
		}

		private static void CalculationC(ref double dTextSize, int nStrlen, TextData textData)
		{
			double fontSize = textData.FontSize;
			if (textData.FontName == "宋体")
			{
				if (object.Equals(fontSize, 6.0) || object.Equals(fontSize, 12.0) || object.Equals(fontSize, 18.0) || object.Equals(fontSize, 24.0) || object.Equals(fontSize, 36.0) || object.Equals(fontSize, 48.0))
				{
					TextCalculation.TextCalculationOne(ref dTextSize, nStrlen, textData);
					return;
				}
				if (object.Equals(fontSize, 8.0) || object.Equals(fontSize, 9.0) || object.Equals(fontSize, 14.0) || object.Equals(fontSize, 20.0) || object.Equals(fontSize, 26.0) || object.Equals(fontSize, 72.0))
				{
					TextCalculation.TextCalculationThree(ref dTextSize, nStrlen, textData);
					return;
				}
				TextCalculation.TextCalculationTwo(ref dTextSize, nStrlen, textData);
				return;
			}
			else if (textData.FontName == "仿宋")
			{
				if (object.Equals(fontSize, 9.0) || object.Equals(fontSize, 16.0) || object.Equals(fontSize, 24.0) || object.Equals(fontSize, 36.0))
				{
					TextCalculation.TextCalculationTwo(ref dTextSize, nStrlen, textData);
					return;
				}
				if (object.Equals(fontSize, 10.0))
				{
					TextCalculation.TextCalculationThree(ref dTextSize, nStrlen, textData);
					return;
				}
				TextCalculation.TextCalculationOne(ref dTextSize, nStrlen, textData);
				return;
			}
			else if (textData.FontName == "黑体")
			{
				if (object.Equals(fontSize, 8.0) || object.Equals(fontSize, 14.0) || object.Equals(fontSize, 20.0) || object.Equals(fontSize, 26.0))
				{
					TextCalculation.TextCalculationTwo(ref dTextSize, nStrlen, textData);
					return;
				}
				TextCalculation.TextCalculationOne(ref dTextSize, nStrlen, textData);
				return;
			}
			else if (textData.FontName == "楷体")
			{
				if (object.Equals(fontSize, 8.0) || object.Equals(fontSize, 10.0) || object.Equals(fontSize, 14.0) || object.Equals(fontSize, 20.0) || object.Equals(fontSize, 26.0))
				{
					TextCalculation.TextCalculationTwo(ref dTextSize, nStrlen, textData);
					return;
				}
				TextCalculation.TextCalculationOne(ref dTextSize, nStrlen, textData);
				return;
			}
			else
			{
				if (!(textData.FontName == "微软雅黑"))
				{
					if (textData.FontName == "RomanD")
					{
						if (object.Equals(fontSize, 6.0) || object.Equals(fontSize, 10.0) || object.Equals(fontSize, 14.0) || object.Equals(fontSize, 18.0) || object.Equals(fontSize, 22.0) || object.Equals(fontSize, 26.0) || object.Equals(fontSize, 48.0) || object.Equals(fontSize, 72.0))
						{
							TextCalculation.TextCalculationThree(ref dTextSize, nStrlen, textData);
							return;
						}
						TextCalculation.TextCalculationOne(ref dTextSize, nStrlen, textData);
					}
					return;
				}
				if (object.Equals(fontSize, 6.0) || object.Equals(fontSize, 9.0) || object.Equals(fontSize, 10.0) || object.Equals(fontSize, 14.0) || object.Equals(fontSize, 18.0) || object.Equals(fontSize, 22.0) || object.Equals(fontSize, 26.0))
				{
					TextCalculation.TextCalculationThree(ref dTextSize, nStrlen, textData);
					return;
				}
				TextCalculation.TextCalculationOne(ref dTextSize, nStrlen, textData);
				return;
			}
		}

		private static void CalculationEOrN(ref double dTextSize, int nStrlen, TextData textData)
		{
			double fontSize = textData.FontSize;
			if (textData.FontName == "宋体")
			{
				if (object.Equals(fontSize, 6.0) || object.Equals(fontSize, 12.0) || object.Equals(fontSize, 18.0) || object.Equals(fontSize, 24.0) || object.Equals(fontSize, 36.0) || object.Equals(fontSize, 48.0))
				{
					TextCalculation.TextCalculationOne(ref dTextSize, nStrlen, textData);
					return;
				}
				if (object.Equals(fontSize, 9.0))
				{
					TextCalculation.TextCalculationTwo(ref dTextSize, nStrlen, textData);
					return;
				}
				TextCalculation.TextCalculationThree(ref dTextSize, nStrlen, textData);
				return;
			}
			else if (textData.FontName == "仿宋")
			{
				if (object.Equals(fontSize, 6.0) || object.Equals(fontSize, 8.0) || object.Equals(fontSize, 14.0) || object.Equals(fontSize, 20.0) || object.Equals(fontSize, 22.0) || object.Equals(fontSize, 28.0) || object.Equals(fontSize, 48.0))
				{
					TextCalculation.TextCalculationOne(ref dTextSize, nStrlen, textData);
					return;
				}
				if (object.Equals(fontSize, 10.0))
				{
					TextCalculation.TextCalculationTwo(ref dTextSize, nStrlen, textData);
					return;
				}
				TextCalculation.TextCalculationThree(ref dTextSize, nStrlen, textData);
				return;
			}
			else if (textData.FontName == "黑体")
			{
				if (object.Equals(fontSize, 6.0) || object.Equals(fontSize, 12.0) || object.Equals(fontSize, 18.0) || object.Equals(fontSize, 24.0) || object.Equals(fontSize, 36.0) || object.Equals(fontSize, 48.0) || object.Equals(fontSize, 72.0))
				{
					TextCalculation.TextCalculationOne(ref dTextSize, nStrlen, textData);
					return;
				}
				if (object.Equals(fontSize, 11.0))
				{
					TextCalculation.TextCalculationFour(ref dTextSize, nStrlen, textData);
					return;
				}
				TextCalculation.TextCalculationThree(ref dTextSize, nStrlen, textData);
				return;
			}
			else if (textData.FontName == "楷体")
			{
				if (object.Equals(fontSize, 6.0) || object.Equals(fontSize, 11.0) || object.Equals(fontSize, 12.0) || object.Equals(fontSize, 18.0) || object.Equals(fontSize, 24.0) || object.Equals(fontSize, 36.0) || object.Equals(fontSize, 48.0) || object.Equals(fontSize, 72.0))
				{
					TextCalculation.TextCalculationOne(ref dTextSize, nStrlen, textData);
					return;
				}
				TextCalculation.TextCalculationThree(ref dTextSize, nStrlen, textData);
				return;
			}
			else
			{
				if (!(textData.FontName == "微软雅黑"))
				{
					if (textData.FontName == "RomanD")
					{
						if (object.Equals(fontSize, 9.0) || object.Equals(fontSize, 14.0) || object.Equals(fontSize, 24.0) || object.Equals(fontSize, 28.0) || object.Equals(fontSize, 72.0))
						{
							TextCalculation.TextCalculationOne(ref dTextSize, nStrlen, textData);
							return;
						}
						TextCalculation.TextCalculationThree(ref dTextSize, nStrlen, textData);
					}
					return;
				}
				if (object.Equals(fontSize, 9.0) || object.Equals(fontSize, 24.0))
				{
					TextCalculation.TextCalculationThree(ref dTextSize, nStrlen, textData);
					return;
				}
				if (object.Equals(fontSize, 14.0) || object.Equals(fontSize, 16.0) || object.Equals(fontSize, 18.0) || object.Equals(fontSize, 20.0) || object.Equals(fontSize, 28.0) || object.Equals(fontSize, 48.0))
				{
					TextCalculation.TextCalculationOne(ref dTextSize, nStrlen, textData);
					return;
				}
				TextCalculation.TextCalculationTwo(ref dTextSize, nStrlen, textData);
				return;
			}
		}

		public static void TextCalculationOne(ref double dTextSize, int nStrlen, TextData textData)
		{
			dTextSize += textData.Equidistant;
			for (int i = 2; i < nStrlen + 1; i++)
			{
				dTextSize += textData.Equidistant;
			}
		}

		public static void TextCalculationTwo(ref double dTextSize, int nStrlen, TextData textData)
		{
			dTextSize += textData.FirstTextSize;
			int num = 0;
			for (int i = 1; i < nStrlen; i++)
			{
				num++;
				if (num > textData.Index)
				{
					num = 1;
				}
				if (num == textData.Index)
				{
					dTextSize += textData.AddSize;
				}
				else
				{
					dTextSize += textData.AddSize + 1.0;
				}
			}
		}

		public static void TextCalculationThree(ref double dTextSize, int nStrlen, TextData textData)
		{
			dTextSize += textData.FirstTextSize;
			int num = 0;
			for (int i = 1; i < nStrlen; i++)
			{
				num++;
				if (num > textData.Index)
				{
					num = 1;
				}
				if (num == textData.Index)
				{
					dTextSize += textData.AddSize + 1.0;
				}
				else
				{
					dTextSize += textData.AddSize;
				}
			}
		}

		public static void TextCalculationFour(ref double dTextSize, int nStrlen, TextData textData)
		{
			dTextSize += textData.FirstTextSize;
			for (int i = 1; i < nStrlen; i++)
			{
				if (i == 2)
				{
					dTextSize += textData.AddSize + 1.0;
				}
				else
				{
					dTextSize += textData.AddSize;
				}
			}
		}
	}
}
